﻿using System;
using RPC;
using System.IO;

namespace Playground {
	public class Example2 {
		public Example2() {
			RPCManager manager = new RPCManager();//Create RPC Manager
			manager.Scan(typeof(RPCDateTimeReply));//Add known message



			String filename = Path.GetTempFileName();
			using (Stream stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) {//Open temporary file.
				for (int i = 0; i < 10; i++) {
					DateTime date = DateTime.Now.AddDays(i);//Create variable
					RPCDateTimeReply msg = new RPCDateTimeReply();//Create message
					msg.Date = date;//Setup variables

					manager.Write(stream, msg);//Send Message
				}
			}


			//Setup events
			manager.AddHandler<RPCDateTimeReply>(OnRPCDateTimeReply);//Add handler, which listens for specific message.

			using (Stream stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None)) {//Open temporary file.
				RPCMessage msg = null;//Setup temporary message
				while ((msg = manager.Read(stream)) != null) {//Loop through stream
					manager.RaiseEvent(msg);//Raise events
				}
			}

			File.Delete(filename);
		}
		public void OnRPCDateTimeReply(RPCDateTimeReply msg) {
			Console.WriteLine("Found Message, date: {0};", msg.Date.ToString());
		}
	}
}
